//
//  xsHadAddMaterialListCollectionView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/30.
//

#import "xsHadAddMaterialListCollectionView.h"
  
@interface xsHadAddMaterialListCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource>



//collectionType;// 0全部 3图片 1视频
// 0时 fileType传@""  3时fileType传@"3" 1时fileType传@"1"
@property (nonatomic,assign)NSInteger collectionType;//

@property (nonatomic,strong) UIView *tableEmptyView;
@property (nonatomic,weak) UIImageView * tableEmptyImage;
@property (nonatomic,weak) UILabel * tableEmptyLabel;
@property (nonatomic,weak) UIButton *covertableEmptyViewButton;

@property (nonatomic,assign)NSInteger page;

@end

@implementation xsHadAddMaterialListCollectionView

//- (NSMutableArray *)materialDataArray{
//    if(!_materialDataArray){
//        _materialDataArray = [[NSMutableArray alloc] init];
//    }
//    return _materialDataArray;
//}
//collectionType;// 0全部 3图片 1视频
// 0时 fileType传@""  3时fileType传@"3" 1时fileType传@"1"
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout withCollectionType:(NSInteger)collectionType{
    
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if(self){
        self.delegate = self;
        self.dataSource = self;
        self.page = 1;
        self.backgroundColor = UIColor.clearColor;//COLOR_VCViewBgcolor;
        self.collectionType = collectionType;
        //self.materialDataArray = materialDataArray;
        
        //        //注册cell
        [self registerClass:[xsHadAddMaterialListCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([xsHadAddMaterialListCollectionViewCell class])];
        [self registerClass:[xsHadAddMaterialListCollectionViewHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([xsHadAddMaterialListCollectionViewHeadView class])];
        
        [self addSubview:self.tableEmptyView];
        [self.tableEmptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            //            make.size.mas_equalTo(CGSizeMake(90, 90+5+21));
            make.size.mas_equalTo(CGSizeMake(250, 250));
        }];
        
        //[self reloadData];
        
//        xsSelectAddMaterialCollectionViewCellModel *md = [xsSelectAddMaterialCollectionViewCellModel new];
//        md.url = @"bossIPDefaultImage";
////        if(i!=0){
////            md.isAddType = NO;
////        }
////        else{
////            md.isAddType = YES;
////        }
//        md.isAddType = YES;
//        [self.materialDataArray addObject:md];
        
        
//        XSWeakSelf;
//        self.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
//            
//            weakSelf.page = 1;
//            [weakSelf.materialDataArray removeAllObjects];
//            
////            xsSelectAddMaterialCollectionViewCellModel *md = [xsSelectAddMaterialCollectionViewCellModel new];
////            md.url = @"bossIPDefaultImage";
////    //        if(i!=0){
////    //            md.isAddType = NO;
////    //        }
////    //        else{
////    //            md.isAddType = YES;
////    //        }
////            md.isAddType = YES;
////            [weakSelf.materialDataArray addObject:md];
//            
//            
//            [weakSelf getAudioDataWithCollectionType:self.collectionType];
//            
//        }];

//        self.mj_footer= [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
//            ++self.page;
//            [self getNumberPeopleDataWithAspectRatio:aspectRatio withCategoryId:categoryId];
//        }];
        
        
//         [self getAudioDataWithCollectionType:self.collectionType];
        
//        [self.mj_header beginRefreshing];
        
        
       
    }
    
    return self;
}

#pragma mark - UICollectionViewDataSource
//一共有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//每一组有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    self.tableEmptyView.hidden = self.materialDataArray.count;
    return self.materialDataArray.count;
}

//每一个cell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    xsHadAddMaterialListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([xsHadAddMaterialListCollectionViewCell class]) forIndexPath:indexPath];
    
    if (indexPath.row<self.materialDataArray.count) {
        cell.model = self.materialDataArray[indexPath.item];
        
    }
    XSWeakSelf;
    cell.xsHadAddMaterialListCellTopXbuttonBeClickCallBackBk = ^(xsSelectAddMaterialCollectionViewCellModel * _Nonnull model, NSInteger collectionType) {
        
        if([self.delegateConfigurationMusic respondsToSelector:@selector(xsHadAddMaterialListCollectionCellTopXbuttonBeClick:withCollectionType:)]){
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.delegateConfigurationMusic xsHadAddMaterialListCollectionCellTopXbuttonBeClick:model withCollectionType:collectionType];
            });
        }
        
    };
    cell.backgroundColor = UIColor.grayColor;
    cell.contentView.backgroundColor = UIColor.clearColor;
    cell.layer.cornerRadius = 6.0;
    cell.layer.masksToBounds = YES;
    
    return cell;
}

//和tableView差不多, 可设置头部和尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        xsHadAddMaterialListCollectionViewHeadView *headerView = [self dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([xsHadAddMaterialListCollectionViewHeadView class]) forIndexPath:indexPath];
        headerView.backgroundColor = UIColor.clearColor;
        return headerView;
    }
//    else if([kind isEqualToString:UICollectionElementKindSectionFooter])
//    {
//        
//    }
    return nil;
}


//每一个分组的上左下右间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
}

//头部视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    return CGSizeMake(self.frame.size.width, 45.0);
    return CGSizeMake(self.frame.size.width, 0.10);
}


//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat margin = 5.0*5.0;
    CGFloat itemW =  (self.frame.size.width-margin)*0.25;
    CGFloat itemH = itemW;
    return CGSizeMake(itemW, itemH);
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return 4;
//}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return 5;
//}

//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item<self.materialDataArray.count){
        xsSelectAddMaterialCollectionViewCellModel *model = self.materialDataArray[indexPath.item];
        
        
        for (xsSelectAddMaterialCollectionViewCellModel *modelItem in self.materialDataArray) {
            if(modelItem.Id == model.Id){
                modelItem.isSelectModel = !modelItem.isSelectModel;
                
                [self reloadItemsAtIndexPaths:@[indexPath]];
                break;
            }
        }
        
        NSLog(@"model.fileName==%@\n",model.fileName);
        XSLogMethod
        if([self.delegateConfigurationMusic respondsToSelector:@selector(xsHadAddMaterialListCollectionCellBeClick: withCollectionType:)]){
            [self.delegateConfigurationMusic xsHadAddMaterialListCollectionCellBeClick:model withCollectionType:self.collectionType];
        }
    }
}

//
////在开始移动是调动此代理方法
//- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"开始移动");
//    return YES;
//}

////在移动结束的时候调用此代理方法
//- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//    NSLog(@"结束移动");
//
//}

//collectionType;// 0全部 3图片 1视频
// 0时 fileType传@""  3时fileType传@"3" 1时fileType传@"1"
//- (void)getAudioDataWithCollectionType:(NSInteger)collectionType
//{
////
//    
//    XSWeakSelf;
//
//    
////    NSString *tempUrl = @"https://mall219.writemall.com/videoapi/disk/files";
//    
//    NSString *tempUrl = [NSString stringWithFormat:@"%@%@",API_BASEURL_BossIPvideo,@"disk/files"];
//    
////page: this.page,
////        limit: 20,
////        fileType: "",
//    
//    NSString *fileTypestr = [NSString stringWithFormat:@"%ld",collectionType];
//    if(collectionType==0){
//        fileTypestr = [NSString new];
//    }
//    NSDictionary *pramerDict = @{@"page":[NSString stringWithFormat:@"%ld",self.page],
//                                 @"limit":@"100",
//                                 @"fileType":fileTypestr
//    };
//    [[ZYFDataManager sharedInstance] POST:tempUrl dict:pramerDict succeed:^(id data) {
//        
//        NSString *temppramerDict = [NSString stringWithFormat:@"%@",pramerDict];
//        XSLogMethod2(temppramerDict)
//        
//        if(collectionType==0){
//            NSLog(@"全部--data==%@\n",data);
//        }
//        else if(collectionType==3){
//            NSLog(@"图片--data==%@\n",data);
//        }
//        else{
//            NSLog(@"视频--data==%@\n",data);
//        }
//        NSDictionary *dictData = data;
//        if([dictData isKindOfClass:[NSDictionary class]]){
//            if([dictData[@"code"] isKindOfClass:[NSNumber class]]||[dictData[@"code"] isKindOfClass:[NSString class]]){
//                NSInteger codeInt = [dictData[@"code"] integerValue];
//                if(codeInt==200){
//                    
//                    if([dictData[@"data"] isKindOfClass:[NSDictionary class]]){
//                        
//                        NSDictionary *xsoutDict = dictData[@"data"] ;
//                        if([xsoutDict[@"data"] isKindOfClass:[NSArray class]]){
//                            NSArray *dataArr3  = xsoutDict[@"data"];
//                            
//                            if(dataArr3.count>0){
//                                for (int ii=0; ii<dataArr3.count; ii++) {
//                                    NSDictionary *tempFinalDict = dataArr3[ii];
//                                    xsSelectAddMaterialCollectionViewCellModel *md = [xsSelectAddMaterialCollectionViewCellModel yy_modelWithDictionary:tempFinalDict];
//                                    //md.url = tempFinalDict[@"url"];
//                                    
//                                    NSString *urlstring = md.url;
//                                    if([urlstring containsString:@".mp4"]){
//                                        NSString *furlString2 = [NSString stringWithFormat:@"%@?x-oss-process=video/snapshot,t_0",urlstring];
//                                        md.url = furlString2;
//                                    }
//                                    if([tempFinalDict[@"id"] isKindOfClass:[NSNumber class]]||
//                                       [tempFinalDict[@"id"] isKindOfClass:[NSString class]]){
//                                       NSString *tempIdStr = [NSString stringWithFormat:@"%ld",[tempFinalDict[@"id"] integerValue]];
//                                        md.Id = [tempIdStr integerValue];
//                                    }
//    //                                if([tempFinalDict[@"description"] isKindOfClass:[NSString class]]){
//    //                                   NSString *tempDescriptionStr = tempFinalDict[@"description"];
//    //                                    md.Description = tempDescriptionStr;
//    //                                }
//                                    md.collectionType = weakSelf.collectionType;
//                                    
//                                    [weakSelf.materialDataArray addObject:md];
//                                }
//                                
//                                dispatch_async(dispatch_get_main_queue(), ^{
//                                    [weakSelf stopEndRefresh:weakSelf.materialDataArray];
//                                });
//                            }
//                            else{
//                                dispatch_async(dispatch_get_main_queue(), ^{
//                                    [weakSelf stopEndRefresh:nil];
//                                });
//                            }
//                        }
//                        else{
//                            dispatch_async(dispatch_get_main_queue(), ^{
//                                [weakSelf stopEndRefresh:nil];
//                            });
//                        }
//                    }
//                    else{
//                        dispatch_async(dispatch_get_main_queue(), ^{
//                            [weakSelf stopEndRefresh:nil];
//                        });
//                    }
//
//                }
//                else{
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        [weakSelf stopEndRefresh:nil];
//                    });
//                }
//            }
//            else{
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    [weakSelf stopEndRefresh:nil];
//                });
//            }
//        }
//        else{
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [weakSelf stopEndRefresh:nil];
//            });
//        }
//    } failure:^(NSDictionary *error) {
//        NSLog(@"getAudioDataWithAspectRatio--error.description==%@\n",error.description);
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [weakSelf stopEndRefresh:nil];
//        });
//    }];
////    [[ZYFDataManager sharedInstance] POST:@"https://mall219.writemall.com/bossapi/speech/voiceTemplateList" dict:pramerDict succeed:^(id data) {} failure:^(NSDictionary *error) {}];
//}


- (UIView *)tableEmptyView{
    
    if (!_tableEmptyView) {
        _tableEmptyView = [[UIView alloc] init];
        _tableEmptyView.backgroundColor = UIColor.clearColor;
        
        
        UITapGestureRecognizer *tableEmptyViewRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableEmptyImageGestureEvent:)];
        [_tableEmptyView addGestureRecognizer:tableEmptyViewRecognizer];
        
        UIImageView *tableEmptyImage = [[UIImageView alloc] init];
        tableEmptyImage.image = [UIImage imageNamed:@"CYNotSearchResultData"];
        [_tableEmptyView addSubview:tableEmptyImage];
        tableEmptyImage.backgroundColor = UIColor.clearColor;
        self.tableEmptyImage = tableEmptyImage;
        
        UILabel *tableEmptyLabel = [[UILabel alloc] init];
        tableEmptyLabel.text = @"暂无数据";//kLocalizedString(@"暂无数据");
        tableEmptyLabel.font = [UIFont systemFontOfSize:16];//Font(16);
        tableEmptyLabel.numberOfLines = 0;
        tableEmptyLabel.textAlignment = NSTextAlignmentCenter;
        tableEmptyLabel.textColor = COLOR_HEX(0x999999);
        [_tableEmptyView addSubview:tableEmptyLabel];
        self.tableEmptyLabel = tableEmptyLabel;
        
        tableEmptyImage.userInteractionEnabled = YES;
        tableEmptyLabel.userInteractionEnabled = YES;

        UITapGestureRecognizer *tableEmptyImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableEmptyImageGestureEvent:)];
        [tableEmptyImage addGestureRecognizer:tableEmptyImageRecognizer];

        UITapGestureRecognizer *tableEmptyLabelRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableEmptyLabelGestureEvent:)];
        [tableEmptyLabel addGestureRecognizer:tableEmptyLabelRecognizer];
        
        [tableEmptyImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_tableEmptyView);
            make.centerY.equalTo(_tableEmptyView.mas_centerY).offset(-10);
            make.size.mas_equalTo(CGSizeMake(80.55, 86.58));
        }];
        [tableEmptyLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_tableEmptyView);
            make.top.equalTo(tableEmptyImage.mas_bottom).offset(5);
        }];
        //
        //        UIButton *covertableEmptyViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //        covertableEmptyViewButton.backgroundColor = UIColor.purpleColor;//UIColor.clearColor;
        //        [_tableEmptyView addSubview:covertableEmptyViewButton];
        //        self.covertableEmptyViewButton = covertableEmptyViewButton;
        //        XSWeakSelf;
        //        [covertableEmptyViewButton addTapRunWithBlock:^(UIButton * _Nonnull YSBtn) {
        //            if(weakSelf.tableViewNotContentDataBgViewBeClickEvent){
        //                weakSelf.tableViewNotContentDataBgViewBeClickEvent();
        //            }
        //        }];
        
    }
    return _tableEmptyView;
    
}
- (void)tableEmptyImageGestureEvent:(UITapGestureRecognizer *)sender{
    
    [self tableEmptyLabelGestureEvent:sender];
}
- (void)tableEmptyLabelGestureEvent:(UITapGestureRecognizer *)sender{
    
    NSLog(@"tableEmptyLabelGestureEvent");
    
    if(self.materialDataArray.count<=0){
        //[self getAudioDataWithCollectionType:self.collectionType];
        [self.mj_header beginRefreshing];
    }
}


#pragma mark - stopEndRefresh
- (void)stopEndRefresh:(NSArray *) array {
    
    if (self.page  == 1) {
        if (self.mj_footer.isRefreshing) {
            [self.mj_footer endRefreshing];
        }
        [self.mj_header endRefreshing];
        [self.mj_footer resetNoMoreData];
    }else {
        if (array.count > 1) {
            [self.mj_footer endRefreshing];
        }else {
            [self.mj_footer endRefreshingWithNoMoreData];
        }
    }
    
    __weak typeof(self) weakself = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakself reloadData];
    });
}

//- (void)xsHadAddMaterialListCollectionDeleteWithModel:(xsSelectAddMaterialCollectionViewCellModel *)dModel withCollectionType:(NSInteger)collecionTyp{
//    
//    
//   
//}
@end

